package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.MessageTypes;
import com.example.mall.dto.MessageTypesDto;
import com.example.mall.mapper.MessageTypesMapper;
import com.example.mall.query.MessageTypesQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IMessageTypesService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 消息类型表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class MessageTypesServiceImpl extends ServiceImpl<MessageTypesMapper, MessageTypes> implements IMessageTypesService {

    @Autowired
    private MessageTypesMapper messageTypesMapper;

    @Override
    public PageDTO<MessageTypesDto> queryPage(MessageTypesQuery messageTypesQuery) {
        if (messageTypesQuery == null) {
            messageTypesQuery = new MessageTypesQuery();
        }
        Page<MessageTypes> page = messageTypesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<MessageTypes> cartPage = lambdaQuery()
                .like(messageTypesQuery.getTypeId()!= null, MessageTypes::getTypeId, messageTypesQuery.getTypeId())
                .like(messageTypesQuery.getDescription()!= null, MessageTypes::getDescription, messageTypesQuery.getDescription())
                .like(messageTypesQuery.getName()!= null, MessageTypes::getName, messageTypesQuery.getName())
                .orderByDesc(MessageTypes::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, MessageTypesDto.class);
    }

    @Override
    public MessageTypes queryMessageTypesById(Long typeId) {
        AssertUtil.isNotNull(typeId, "typeId不能为空");
        return messageTypesMapper.selectById(typeId);
    }

    @Override
    public List<MessageTypes> queryMessageTypesList() {
        return this.list();
    }

    @Override
    public void insert(MessageTypesDto messageTypesDto) {
        MessageTypes messageTypes = new MessageTypes();
        CopyUtils.copyProperties(messageTypesDto, messageTypes);
        messageTypes.setTypeId(CodeGenerateUtils.generateUniqueId());
        messageTypes.setCreateDate(new Date());
        messageTypes.setUpdateDate(new Date());
        messageTypesMapper.insert(messageTypes);
    }

    @Override
    public void update(MessageTypesDto messageTypesDto) {
        AssertUtil.isNotNull(messageTypesDto.getTypeId(), "typeId不能为空");
        MessageTypes messageTypes = messageTypesMapper.selectById(messageTypesDto.getTypeId());
        AssertUtil.isNotNull(messageTypes, "消息类型不存在");
        CopyUtils.copyProperties(messageTypesDto, messageTypes);
        messageTypes.setUpdateDate(new Date());
        messageTypesMapper.updateById(messageTypes);
    }

    @Override
    public void delete(Long typeId) {
        AssertUtil.isNotNull(typeId, "typeId不能为空");
        MessageTypes messageTypes = messageTypesMapper.selectById(typeId);
        AssertUtil.isNotNull(messageTypes, "消息类型不存在");
        messageTypesMapper.deleteById(typeId);
    }

    @Override
    public void deleteBatch(List<Long> typeIds) {
        AssertUtil.isNotNull(typeIds, "typeIds不能为空");
        List<MessageTypes> messageTypes = messageTypesMapper.selectBatchIds(typeIds);
        AssertUtil.isNotNull(messageTypes, "消息类型不存在");
        messageTypesMapper.deleteBatchIds(typeIds);
    }
}
